package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hmdp.utils.RedisConstants.USER_FOLLOW_USER;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    IUserService userService;

    @Override
    public Result follow(Long id, Boolean flag) {
        Long userId = UserHolder.getUser().getId();
        boolean save=false,remove=false;
        String key = USER_FOLLOW_USER + userId;
        String followId = id.toString();
        //关注添加数据
        //取关删除数据
        if (BooleanUtil.isTrue(flag)){
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            save = save(follow);
            //在redis中存入关注的人
            stringRedisTemplate.opsForSet().add(key, followId);
        }else{
            remove = remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", id));
            stringRedisTemplate.opsForSet().remove(key,followId);
        }
        if (save||remove) return Result.ok();
        return Result.fail("关注/取关-------失败");
    }

    @Override
    public Result followOrNot(Long id) {
        //查询是否存在
        Long userId = UserHolder.getUser().getId();
        Integer count = query().eq("user_id", userId).eq("follow_user_id", id).count();
        return Result.ok(count>0);
    }

    @Override
    public Result followCommon(Long id) {
        Long userId = UserHolder.getUser().getId();
        //使用redis进行交集查询
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(USER_FOLLOW_USER + userId, USER_FOLLOW_USER + id);
        if(CollectionUtil.isEmpty(intersect)) return Result.ok(Collections.emptyList());
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<User> users = userService.query().in("id", ids).list();
//        map里面是函数式编程
        List<UserDTO> userDTOS = users.stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());

        return Result.ok(userDTOS);
    }
}
